// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdint.h>

#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
#include "content/renderer/media/gpu/rtc_video_encoder.h"
#include "media/renderers/mock_gpu_video_accelerator_factories.h"
#include "media/video/mock_video_encode_accelerator.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/libyuv/include/libyuv/planar_functions.h"
#include "third_party/webrtc/modules/video_coding/include/video_codec_interface.h"

using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::SaveArg;
using ::testing::Values;
using ::testing::WithArgs;

namespace content {

namespace {

    const int kInputFrameFillY = 12;
    const int kInputFrameFillU = 23;
    const int kInputFrameFillV = 34;
    const unsigned short kInputFrameHeight = 234;
    const unsigned short kInputFrameWidth = 345;
    const unsigned short kStartBitrate = 100;

} // anonymous namespace

class RTCVideoEncoderTest
    : public ::testing::TestWithParam<webrtc::VideoCodecType> {
public:
    RTCVideoEncoderTest()
        : mock_gpu_factories_(
            new media::MockGpuVideoAcceleratorFactories(nullptr))
        , encoder_thread_("vea_thread")
        , idle_waiter_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
              base::WaitableEvent::InitialState::NOT_SIGNALED)
    {
    }

    media::MockVideoEncodeAccelerator* ExpectCreateInitAndDestroyVEA()
    {
        // The VEA will be owned by the RTCVideoEncoder once
        // factory.CreateVideoEncodeAccelerator() is called.
        media::MockVideoEncodeAccelerator* mock_vea = new media::MockVideoEncodeAccelerator();

        EXPECT_CALL(*mock_gpu_factories_.get(), DoCreateVideoEncodeAccelerator())
            .WillRepeatedly(Return(mock_vea));
        EXPECT_CALL(*mock_vea, Initialize(_, _, _, _, _))
            .WillOnce(Invoke(this, &RTCVideoEncoderTest::Initialize));
        EXPECT_CALL(*mock_vea, UseOutputBitstreamBuffer(_)).Times(3);
        EXPECT_CALL(*mock_vea, Destroy()).Times(1);
        return mock_vea;
    }

    void SetUp() override
    {
        DVLOG(3) << __func__;
        ASSERT_TRUE(encoder_thread_.Start());

        EXPECT_CALL(*mock_gpu_factories_.get(), GetTaskRunner())
            .WillRepeatedly(Return(encoder_thread_.task_runner()));
        mock_vea_ = ExpectCreateInitAndDestroyVEA();
    }

    void TearDown() override
    {
        DVLOG(3) << __func__;
        EXPECT_TRUE(encoder_thread_.IsRunning());
        RunUntilIdle();
        rtc_encoder_->Release();
        RunUntilIdle();
        encoder_thread_.Stop();
    }

    void RunUntilIdle()
    {
        DVLOG(3) << __func__;
        encoder_thread_.task_runner()->PostTask(
            FROM_HERE, base::Bind(&base::WaitableEvent::Signal, base::Unretained(&idle_waiter_)));
        idle_waiter_.Wait();
    }

    void CreateEncoder(webrtc::VideoCodecType codec_type)
    {
        DVLOG(3) << __func__;
        rtc_encoder_ = base::MakeUnique<RTCVideoEncoder>(codec_type,
            mock_gpu_factories_.get());
    }

    // media::VideoEncodeAccelerator implementation.
    bool Initialize(media::VideoPixelFormat input_format,
        const gfx::Size& input_visible_size,
        media::VideoCodecProfile output_profile,
        uint32_t initial_bitrate,
        media::VideoEncodeAccelerator::Client* client)
    {
        DVLOG(3) << __func__;
        client->RequireBitstreamBuffers(0, input_visible_size,
            input_visible_size.GetArea());
        return true;
    }

    webrtc::VideoCodec GetDefaultCodec()
    {
        webrtc::VideoCodec codec = {};
        memset(&codec, 0, sizeof(codec));
        codec.width = kInputFrameWidth;
        codec.height = kInputFrameHeight;
        codec.codecType = webrtc::kVideoCodecVP8;
        codec.startBitrate = kStartBitrate;
        return codec;
    }

    void FillFrameBuffer(rtc::scoped_refptr<webrtc::I420Buffer> frame)
    {
        CHECK(libyuv::I420Rect(frame->MutableDataY(), frame->StrideY(),
                  frame->MutableDataU(), frame->StrideU(),
                  frame->MutableDataV(), frame->StrideV(), 0, 0,
                  frame->width(), frame->height(), kInputFrameFillY,
                  kInputFrameFillU, kInputFrameFillV)
            == 0);
    }

    void VerifyEncodedFrame(const scoped_refptr<media::VideoFrame>& frame,
        bool force_keyframe)
    {
        DVLOG(3) << __func__;
        EXPECT_EQ(kInputFrameWidth, frame->visible_rect().width());
        EXPECT_EQ(kInputFrameHeight, frame->visible_rect().height());
        EXPECT_EQ(kInputFrameFillY,
            frame->visible_data(media::VideoFrame::kYPlane)[0]);
        EXPECT_EQ(kInputFrameFillU,
            frame->visible_data(media::VideoFrame::kUPlane)[0]);
        EXPECT_EQ(kInputFrameFillV,
            frame->visible_data(media::VideoFrame::kVPlane)[0]);
    }

protected:
    media::MockVideoEncodeAccelerator* mock_vea_;
    std::unique_ptr<RTCVideoEncoder> rtc_encoder_;

private:
    std::unique_ptr<media::MockGpuVideoAcceleratorFactories> mock_gpu_factories_;
    base::Thread encoder_thread_;
    base::WaitableEvent idle_waiter_;
};

TEST_P(RTCVideoEncoderTest, CreateAndInitSucceeds)
{
    const webrtc::VideoCodecType codec_type = GetParam();
    CreateEncoder(codec_type);
    webrtc::VideoCodec codec = GetDefaultCodec();
    codec.codecType = codec_type;
    EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, rtc_encoder_->InitEncode(&codec, 1, 12345));
}

TEST_P(RTCVideoEncoderTest, RepeatedInitSucceeds)
{
    const webrtc::VideoCodecType codec_type = GetParam();
    CreateEncoder(codec_type);
    webrtc::VideoCodec codec = GetDefaultCodec();
    codec.codecType = codec_type;
    EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, rtc_encoder_->InitEncode(&codec, 1, 12345));

    ExpectCreateInitAndDestroyVEA();
    EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, rtc_encoder_->InitEncode(&codec, 1, 12345));
}

TEST_F(RTCVideoEncoderTest, EncodeScaledFrame)
{
    const webrtc::VideoCodecType codec_type = webrtc::kVideoCodecVP8;
    CreateEncoder(codec_type);
    webrtc::VideoCodec codec = GetDefaultCodec();
    EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, rtc_encoder_->InitEncode(&codec, 1, 12345));

    EXPECT_CALL(*mock_vea_, Encode(_, _))
        .Times(2)
        .WillRepeatedly(Invoke(this, &RTCVideoEncoderTest::VerifyEncodedFrame));

    const rtc::scoped_refptr<webrtc::I420Buffer> buffer = webrtc::I420Buffer::Create(kInputFrameWidth, kInputFrameHeight);
    FillFrameBuffer(buffer);
    std::vector<webrtc::FrameType> frame_types;
    EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
        rtc_encoder_->Encode(
            webrtc::VideoFrame(buffer, 0, 0, webrtc::kVideoRotation_0),
            nullptr, &frame_types));

    const rtc::scoped_refptr<webrtc::I420Buffer> upscaled_buffer = webrtc::I420Buffer::Create(2 * kInputFrameWidth, 2 * kInputFrameHeight);
    FillFrameBuffer(upscaled_buffer);
    EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
        rtc_encoder_->Encode(webrtc::VideoFrame(upscaled_buffer, 0, 0,
                                 webrtc::kVideoRotation_0),
            nullptr, &frame_types));
}

INSTANTIATE_TEST_CASE_P(CodecProfiles,
    RTCVideoEncoderTest,
    Values(webrtc::kVideoCodecVP8,
        webrtc::kVideoCodecH264));
} // namespace content
